package com.ky.user.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ky.common.config.SystemConfig;
import com.ky.common.param.UserLogin;

import com.ky.common.param.UserAdd;


import com.ky.common.util.JwtUtil;
import com.ky.common.vo.R;
import com.ky.user.entity.User;


import com.ky.user.mapper.UserMapper;
import com.ky.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wgz
 * @since 2024-06-17
 */
@RestController
@RequestMapping("/user/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;

    @RequestMapping("/selectUser/{userId}")
    public R selectUser(@PathVariable("userId") int userId) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.in("id", userId);
        return R.ok(userMapper.selectOne(userQueryWrapper));
    }

    @RequestMapping("/getUser")
    public R getUser(HttpServletRequest request) {
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("id",id);
        return R.ok(userMapper.selectOne(userQueryWrapper));
    }

    /**
     * 使用账户和密码登录
     *
     * @param userLogin
     * @return
     */
    @RequestMapping("/loginpass")
    public R loginPass(@RequestBody UserLogin userLogin) {
        return userService.loginPass(userLogin);
    }

    /**
     * 使用手机号和验证码登录
     *
     * @param userLogin
     * @return
     */
    @RequestMapping("/logincode")
    public R loginCode(@RequestBody UserLogin userLogin) {
        return userService.loginCode(userLogin);
    }



    @RequestMapping("/register")
    public R register(HttpServletRequest request, @RequestBody UserAdd userAdd){
        return userService.register(request,userAdd);
    }
     @RequestMapping("/logout")
    public R logout(HttpServletRequest request){
         String token = request.getHeader(SystemConfig.HEADER_TOKEN);
         return userService.logout(token);
     }
    @RequestMapping("/findAllFollows")
    public R findAllFollows(){
        return userService.findAllFollows();
    }
    @RequestMapping("/findByMapFollows")
    public R findByMapFollows(String username){
        return userService.findByMapFollows(username);
    }
    @RequestMapping("/findById")
    public R findById(HttpServletRequest request){
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        return userService.findById(id);
    }
    @PostMapping("/save")
    public R save(@RequestBody User user,HttpServletRequest request) {
        return userService.saveUser(user,request);
    }
    @PostMapping("/updatePassword")
    public R updatePassword(@RequestBody User user,HttpServletRequest request) {
        return userService.updatePassword(user,request);
    }

    @PostMapping("/updatePhone")
    public R updatePhone(@RequestBody User user,HttpServletRequest request) {
        return userService.updatePhone(user,request);
    }


}

